package com.bbs.detai1713.controller;

import com.alibaba.fastjson.JSONObject;
import com.bbs.detai1713.model.AddFavorite;
import com.bbs.detai1713.model.RemoveFavorite;
import com.bbs.detai1713.model.SelectFavorite;
import com.bbs.detai1713.model.SelectFavoriteByPostingId;
import com.bbs.detai1713.service.DataService;
import com.bbs.detai1713.tool.ResultInfo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


@Api(tags = "收藏夹版块")
@Slf4j
@RestController
public class FavController {
    @Autowired
    DataService dataService;

    @PostMapping("/addFavorite")
    public ResultInfo addFavorite(@RequestBody AddFavorite addFavorite, @AuthenticationPrincipal UserDetails user) {
        long userId = Long.parseLong(JSONObject.parseObject(JSONObject.toJSONString(user)).get("userId").toString());
        addFavorite.setUserId(userId);
        addFavorite.setCollectDate(new Date());
        if (dataService.addFavorite(addFavorite) >= 1)
            return ResultInfo.success("添加收藏夹成功!");
        else
            return ResultInfo.failure("添加收藏夹失败!");
    }

    @PostMapping("/selectFavoriteByPostingId")
    public ResultInfo selectFavoriteByPostingId(@RequestBody SelectFavoriteByPostingId selectFavoriteByPostingId, @AuthenticationPrincipal UserDetails user) {
        long userId = Long.parseLong(JSONObject.parseObject(JSONObject.toJSONString(user)).get("userId").toString());
        log.info("进入isCollect:{} {}", userId, selectFavoriteByPostingId);
        log.info("查询结果为:", dataService.selectFavoriteByPostingId(userId, selectFavoriteByPostingId.getPostingId()));
        if (dataService.selectFavoriteByPostingId(userId, selectFavoriteByPostingId.getPostingId()).size() > 0)
            return ResultInfo.success("查询到了");
        else
            return ResultInfo.failure("未查到");

    }

    @GetMapping("/selectFavorite")
    public ResultInfo selectFavorite(@AuthenticationPrincipal UserDetails user) {
        long userId = Long.parseLong(JSONObject.parseObject(JSONObject.toJSONString(user)).get("userId").toString());
        log.info("查询收藏夹,用户id为:{}", userId);
        SelectFavorite selectFavorite = new SelectFavorite();
        selectFavorite.setUserId(userId);
        return ResultInfo.success(dataService.selectFavorite(selectFavorite), "查询收藏夹成功!");
    }

    @PostMapping("/removeFavorite")
    public ResultInfo removeFavorite(@RequestBody RemoveFavorite removeFavorite, @AuthenticationPrincipal UserDetails user) {
        long userId = Long.parseLong(JSONObject.parseObject(JSONObject.toJSONString(user)).get("userId").toString());
        removeFavorite.setUserId(userId);
        log.info("移除收藏夹,数据为:{}", removeFavorite);
        if (dataService.removeFavorite(removeFavorite) >= 1)
            return ResultInfo.success("移除收藏夹成功!");
        else
            return ResultInfo.failure("移除收藏夹失败!");

    }

}
